package org.thotheolh.sweatkeeper.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import org.thotheolh.sweatkeeper.helper.Workout;
import org.thotheolh.sweatkeeper.util.Resource;

public class WorkoutPanel extends JPanel {
	
	private JToolBar toolBar;
	private JButton btnNewWorkout;
	private JButton btnLoadWorkout;
	private JPanel contentPanel;
	private JTable table;
	private JButton btnEditWorkout;
	private JButton btnDeleteWorkout;
	private MyDefaultTableModel model;
	

	/**
	 * Create the panel.
	 */
	public WorkoutPanel() {
		setLayout(new BorderLayout(0, 0));
		
		toolBar = new JToolBar();
		this.add(toolBar, BorderLayout.NORTH);
		
		btnNewWorkout = new JButton("New Workout");
		btnNewWorkout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				NewWorkoutDialog dialog = new NewWorkoutDialog();
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
			}
		});
		toolBar.add(btnNewWorkout);
		
		btnLoadWorkout = new JButton("Load Workout");
		toolBar.add(btnLoadWorkout);
		
		btnEditWorkout = new JButton("Edit Workout");
		toolBar.add(btnEditWorkout);
		
		btnDeleteWorkout = new JButton("Delete Workout");
		toolBar.add(btnDeleteWorkout);
		
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		
		
		add(contentPanel, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		model = new MyDefaultTableModel();
		table.setModel(model);
		model.addColumn("WORKOUT ID");
		model.addColumn("WORKOUT DATE");
		model.addColumn("ENTRY");
		model.addColumn("TYPE");
		model.addColumn("COMMENTs");
		table.getColumnModel().getColumn(0).setPreferredWidth(10);
		table.getColumnModel().getColumn(1).setPreferredWidth(30);
		table.getColumnModel().getColumn(2).setPreferredWidth(30);
		table.getColumnModel().getColumn(3).setPreferredWidth(10);
		scrollPane.setViewportView(table);
		
		initData();
	}
	
	public void initData(){
		model.getDataVector().removeAllElements();
		ArrayList ls = Resource.getInstance().getSql().getAllWorkout();
		if (ls != null && (!ls.isEmpty())){
			for (int i = 0; i < ls.size(); i++){
				Workout tmp = (Workout)ls.get(i);
				String[] row = new String[5];
				row[0] = ""+tmp.getWorkoutId();
				row[1] = ""+tmp.getWorkoutDate();
				row[2] = ""+tmp.getEntryTimestamp();
				row[3] = ""+tmp.getType();
				row[4] = ""+tmp.getComments();
				model.addRow(row);
			}
		}
	}
	
	public class MyDefaultTableModel extends DefaultTableModel {

        public MyDefaultTableModel() {
            super();
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
    }

	
}
